﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="r_comercial_atendimentos.aspx.cs" Inherits="relatorios_r_comercial_atendimentos" %>
<%
    string Permissao_RelatoriosGerais = "" + this.Session["Permissao_RelatoriosGerais"];
    if (Permissao_RelatoriosGerais != "1")
    {
        this.Response.Redirect("../login.aspx");
    }    
%>
        
<div style="background-color: White;">
    <%
        bool hasResults = false;
        DateTime now = DateTime.Now;
        string month = this.Request["month"];
        string year = this.Request["year"];            
        int iMonth = -1;
        int iYear = -1;            
        int maxDay = 31;
            
        // Month
        if (string.IsNullOrEmpty(month) == true)
        {
            iMonth = now.Month;
        }
        else
        {
            try
            {
                iMonth = int.Parse(month);
            }
            catch { iMonth = now.Month; }
        }
        // Year            
        if (string.IsNullOrEmpty(year) == true)
        {
            iYear = now.Year;
        }
        else
        {
            try
            {
                iYear = int.Parse(year);
            }
            catch { iYear = now.Year; }                
        }

        maxDay = DateTime.DaysInMonth(iYear, iMonth);            
        //DateTime targetDate = DateTime.Now;     
    %>
    <center>
        <form>
            <table>
                <tr>
                    <td>Mês</td>
                    <td>Ano</td>
                    <td></td>
                </tr>

                <tr>
                    <td>
                        <input type="text" id="month" name="month" value="<%= iMonth %>" />
                    </td>
                    <td><input type="text" id="year" name="year" value="<%= iYear %>" /></td>
                    <td><input type="submit" value="ok" /></td>
                </tr>
            </table>
        </form>
    </center>
          
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
                ['Dia', 'Atendimentos'],
                <%
                    hasResults = false;
                    PL.DataBase.DAO.Instance.Setup(PL.DataBase.DatabaseTypes.SqlServer, PL.General.Instance.BIConnectionString);
                    PL.DataBase.DAO.Instance.Connector.Open();
    
                    string message = "";
                    try
                    {         
                        DateTime targetDate = new DateTime(iYear, iMonth, 1);   
                        DateTime maxDate = new DateTime(iYear, iMonth, maxDay);
                        string sql = "";
                        /*
                        sql += "SELECT Vendedor, SUM(Total) As tt From BI_Atendimentos_Comerciais ";
                        sql += " WHERE ";                    
                        sql += " Data >= CONVERT(VARCHAR, '" + targetDate.ToString().Split(' ')[0] + "',103) ";
                        sql += " AND Data <= CONVERT(VARCHAR, '" + maxDate.ToString().Split(' ')[0] + "',103) ";
                        sql += " GROUP BY Vendedor ";
                        */

                        sql += " SELECT        COUNT(l.Codigo) AS TotalAtendimentos, u.Nome ";
                        sql += " FROM            logs AS l INNER JOIN  ";
                        sql += "                    sacuser AS u ON l.Cod_Usuario = u.Código ";
                        sql += " WHERE ";
                        sql += " (l.Funcao = 'Realizou atendimento') ";
                        sql += " AND (l.Data >= CONVERT(DATE, '" + targetDate.ToString().Split(' ')[0] + "',103)) ";                        
                        sql += "  AND (l.Data <= CONVERT(DATE, '" + maxDate.ToString().Split(' ')[0] + "',103)) ";                        
                        sql += " AND (u.Cod_Nivel = 2 OR u.Cod_Nivel = 6) AND (u.Código <> 123)";
                        sql += " GROUP BY u.Nome ";
                        sql += " ORDER BY u.Nome ";

                        var reader = PL.DataBase.DAO.Instance.Connector.QueryWithReader(sql);
                        while (reader.Read() == true)
                        {         
                            hasResults = true;   
                            this.Response.Write("['" + reader["Nome"] + "', " + reader["TotalAtendimentos"] + "],");
                        }
                        reader.Close();
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                    }

                    PL.DataBase.DAO.Instance.Connector.Close();
                    this.Response.Write(message);
                %>
            ]);

            <%
                if (hasResults == true)
                {
            %>
                    var options = {
                        title: 'Atendimentos no mês'
                    };
                    var chart = new google.visualization.LineChart(document.getElementById('r_comercial_atendimentos'));
                    chart.draw(data, options);
            <%
                }
            %>
        }
    </script>
    <div id="r_comercial_atendimentos" style="width: 80%; height: 200px;">
        <center>nenhum dado encontrado</center>
    </div>
</div>